package com.logisticsmanagement.services;

import static com.logisticsmanagement.utils.GlobelValue.QUERY_NOT_FOUND;
import static com.logisticsmanagement.utils.GlobelValue.QUERY_NOT_FOUND_DESCRIPTON;
import static com.logisticsmanagement.utils.GlobelValue.QUERY_SUCCESS;
import static com.logisticsmanagement.utils.GlobelValue.QUERY_SUCCESS_DESCRIPTON;

import java.util.ArrayList;
import java.util.List;

import com.logisticsmanagement.dao.RouteDao;
import com.logisticsmanagement.domain.DeliveryStation;
import com.logisticsmanagement.domain.Route;
import com.logisticsmanagement.vo.BaseResponse;
import com.logisticsmanagement.vo.VoDeliveryStation;
import com.logisticsmanagement.vo.VoIndent;
import com.logisticsmanagement.vo.VoRoute;

public class RouteService {

	private RouteDao routeDao=new RouteDao();
	
	
	/**
	 * jundge special route is existed
	 * return ture if exists.
	 */
	public boolean queryRouteIsExit(int startCityID,int endCityID){
		
		Route queryRoute=new Route();
		if(routeDao.selectById(startCityID, endCityID)!=null){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 *to know price per kilogram between startCityID and endCityID
	 * @param startCityID,endCityID
	 * @return
	 */
	public BaseResponse<VoRoute> queryRoute(int startCityID,int endCityID){
		Route route=new Route();
		VoRoute voRoute=new VoRoute();
		List<VoRoute> listVoRoute=new ArrayList<VoRoute>();
		//set default value
		BaseResponse<VoRoute> baseResponseVoRoute=new BaseResponse<VoRoute>(QUERY_NOT_FOUND,
				QUERY_NOT_FOUND_DESCRIPTON,listVoRoute);
		route=routeDao.selectById(startCityID,endCityID);
		voRoute.setStartCityID(route.getStartCityID());
		voRoute.setEndCityID(route.getEndCityID());
		voRoute.setNextCityID(route.getNextCityID());
		voRoute.setPricePerKG(route.getPricePerKG());
		
		listVoRoute.add(voRoute);

		baseResponseVoRoute.setResponseCode(QUERY_SUCCESS);
		baseResponseVoRoute.setResponseDescription(QUERY_SUCCESS_DESCRIPTON);
	
		return baseResponseVoRoute;	
	}
	
	/**
	 * return true if success to modify price
	 * and set price=newPricePerKG
	 * 
	 */
	public boolean modifyRoutePricePerKG(int startCityID,int endCityID,
			double newPricePerKG){
		Route queryRoute=new Route();
		queryRoute=routeDao.selectById(startCityID, endCityID);
		queryRoute.setPricePerKG(newPricePerKG);
		return routeDao.update(queryRoute);
		
	}
	
	/**
	 * return true if update successes
	 * accord startCityID ,endCityID and nextCityID to put startCityID=nextCityID
	 * and through research route table in database to know which is nextCityID
	 */
	public boolean updateRouteInfo(int startCityID,int endCityID,int nextCityID){
		Route queryRoute=new Route();
		queryRoute=routeDao.selectById(startCityID, endCityID);
		queryRoute.setNextCityID(nextCityID);
		return routeDao.update(queryRoute);
	}
	
	/**
	 *by startCityId and endCityID to decide to delete which route 
	 *and set pricePerKG==0 to means it is not arrived.
	 */
	public boolean deleteRoute(int startCityID,int endCityID){
		Route queryRoute=new Route();
		queryRoute=routeDao.selectById(startCityID, endCityID);
		queryRoute.setIsDelete(true);
		return routeDao.update(queryRoute);
		
	}
	
}
